<!--
 * Description: 成本模型计算集合组件
 * Company: 联宝（合肥）电子科技有限公司
 * Author: Qiuxue.Wu
 * Date: 2020/10/13 17:26
-->
<template>
  <div v-loading="isRequesting">
    <PlasticPelletsModel
      v-if="isShowModel('Small Plastic')"
      :commodity="commodity"
      :fdRowId="fdRowId"
      :gcmFunction="gcmFunction"
      :lastModel="lastModel"
    />

    <CartonCostModel
      v-else-if="isShowModel('Outer Carton')"
      :commodity="commodity"
      :fdRowId="fdRowId"
      :gcmFunction="gcmFunction"
      :lastModel="lastModel"
    />
    <EPECushionCostModel
      v-else-if="isShowModel('EPE Cushion')"
      :commodity="commodity"
      :fdRowId="fdRowId"
      :gcmFunction="gcmFunction"
      :lastModel="lastModel"
    />
    <PCushionCostModel
      v-else-if="isShowModel('P-Cushion')"
      :commodity="commodity"
      :fdRowId="fdRowId"
      :gcmFunction="gcmFunction"
      :lastModel="lastModel"
    />
    <div v-else class="empty">
      暂时没有匹配到计算模型
    </div>
  </div>
</template>
<script>
import { modelComputedResultLogQueryResultLog } from '@/api/model/computed-result-log'

import PlasticPelletsModel from './me/PlasticPelletsModel'
import CartonCostModel from './pkg/CartonCostModel'
import EPECushionCostModel from './pkg/EPECushionCostModel'
import PCushionCostModel from './pkg/PCushionCostModel'
export default {
  name: 'CalcModel',

  components: {
    PlasticPelletsModel,
    CartonCostModel,
    EPECushionCostModel,
    PCushionCostModel
  },

  props: {
    commodity: {
      type: String,
      default: ''
    },

    gcmFunction: {
      type: String,
      default: ''
    },

    fdRowId: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      isRequesting: false,

      lastModel: {},

      isRequestEnd: false
    }
  },

  computed: {
    isShowModel() {
      return modelStr => {
        if (this.commodity) {
          return this.commodity.toLowerCase() === modelStr.toLowerCase() && this.isRequestEnd
        }
      }
    }
  },

  created() {
    if (this.fdRowId) {
      this.lastCalcResult()
    } else {
      this.isRequestEnd = true
    }
  },

  methods: {
    // 最近计算结果
    async lastCalcResult() {
      try {
        this.isRequesting = true
        const result = await modelComputedResultLogQueryResultLog(
          {
            fdRowId: this.fdRowId
          },
          { msgError: true }
        )
        if (result.data.computedResult) {
          this.lastModel = { ...result.data.computedResult }
        }
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequestEnd = true
        this.isRequesting = false
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.empty {
  height: 400px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
  color: #409eff;
}
</style>
